Use case: Time base in interrupt mode

HAL1

HAL2

Start TIMx in interrupt mode

  • The function HAL_TIM_Base_Start_IT() is used to enable:

    • The TIMx peripheral.

    • The update event interrupt.

TIMx IRQ handler

  • Inside the TIMx interrupt handler, the user calls the global IRQ handler of the TIM driver HAL_TIM_IRQHandler() to handle the update interrupt request.

Callback function

  • The function HAL_TIM_PeriodElapsedCallback() is called when the period elapses.

Start TIMx in interrupt mode

  • The function HAL_TIM_Start_IT() is used to enable:

    • The TIMx peripheral.

    • The update event interrupt.

TIMx IRQ handler

  • Inside the TIMx interrupt handler, the user can call either:

    • The global IRQ handler HAL_TIM_IRQHandler() to handle the TIMx interrupt request, or

    • The single process IRQ handler for specific interrupt events in this case HAL_TIM_UPD_IRQHandler().

Callback function

  • The function HAL_TIM_UpdateCallback() is called when the period elapses.

Sequence diagram

Timer as basic counter

@startuml
' To add a number by line
'autonumber

title "Timer as basic counter HAL 1"

' Fix order of each column
participant "User Application" as App
participant "HAL TIM" as HAL_TIM
participant "MSP" as MSP
participant "IT" as IT

group Initialization
App -[hidden]>HAL_TIM
end

group Start TIMx in IT mode
App -> HAL_TIM: <b><font color=blue>HAL_TIM_Base_Start_IT</b>()
HAL_TIM -> HAL_TIM: Enable UPDATE interrupt\nEnable TIMx counter
IT <[#red]-] : <font color=red><b>TIMx IRQ
IT -> HAL_TIM: TIMx IRQ
HAL_TIM -> HAL_TIM: HAL_TIM_IRQHandler()
HAL_TIM -> HAL_TIM: Clear flag
HAL_TIM -> App: <b><font color=green>HAL_TIM_PeriodElapsedCallback</b>()
end
@enduml

Code snippet

HAL1

HAL2

/* Declaration of the tim handle */
TIM_HandleTypeDef htim2;

/* Ensure that all required peripherals
(TIM, GPIO, NVIC, etc.) are configured properly
before starting the timer.*/
.............

/* Start the timer TIMx in interrupt mode */
HAL_TIM_Base_Start_IT(&htim2);

/* User must call the Interrupt Handler
  functions in TIMx global interrupt */

void TIM2_IRQHandler(void)
{
    /* Code to execute at each interrupt */
    HAL_TIM_IRQHandler(&htim2);
}

/* Call the callback function */

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
        /* Code to execute at each interrupt
          For example, toggle an LED */
        HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
}
/* Declaration of the tim handle */
hal_tim_handle_t htim2;

/* Ensure that all required peripherals
(TIM, GPIO, NVIC, etc.) are configured properly
before starting the timer.*/
     .............

/* Start the timer TIMx in interrupt mode */
HAL_TIM_Start_IT(&htim2);

/* User must call the Interrupt Handler
 functions in TIM2 global interrupt */

void TIM2_IRQHandler(void)
{
    /* Code to execute at each interrupt */
    HAL_TIM_IRQHandler(&htim2);
}

/* Call the callback function */

void HAL_TIM_UpdateCallback(hal_tim_handle_t *htim)
{
    /* Code to execute at each interrupt
       For example, toggle an LED */
    HAL_GPIO_TogglePin(HAL_GPIOA, HAL_GPIO_PIN_5);
}

Key changes

  1. The time base start API is renamed from HAL_TIM_Base_Start_IT() to HAL_TIM_Start_IT().

  2. The update callback is renamed from HAL_TIM_PeriodElapsedCallback() to HAL_TIM_UpdateCallback().

  3. HAL2 provides a dedicated update IRQ handler HAL_TIM_UPD_IRQHandler() in addition to the global handler HAL_TIM_IRQHandler().